﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Client.App.Services;
using Client.App.ViewModels.Interfaces;
using Client.App.Windows;
using Core.Mvvm.Commands;
using Core.Mvvm.ViewModels;
using Xceed.Wpf.Toolkit;

namespace Client.App.ViewModels.Implementations
{
    public class MainViewModel : ClosableViewModel, IMainViewModel
    {
        #region Fields
        
        private ICommand _configurationCommand;
        private readonly IConfigurationService _configurationService;

        #endregion

        #region Properties

        public ICommand ConfigurationCommand 
        { 
            get { return _configurationCommand ?? (_configurationCommand = new SimpleCommand(OnConfiguration)); }
        }

        public IAlgorithmsViewModel Algorithms { get; private set; }

        public IEfficiencyAccelerationTestViewModel EfficiencyAccelerationTestViewModel { get; private set; }

        public IDataTimeTestViewModel DataTimeTestViewModel { get; private set; }

        #endregion

        #region Constructors

        public MainViewModel(Dispatcher dispatcher, IConfigurationService configurationService, IEfficiencyAccelerationTestViewModel efficiencyAccelerationTestViewModel, 
            IDataTimeTestViewModel dataTimeTestViewModel, IAlgorithmsViewModel algorithms) 
            : base(dispatcher)
        {
            if (ReferenceEquals(efficiencyAccelerationTestViewModel, null))
            {
                throw new ArgumentNullException("efficiencyAccelerationTestViewModel");
            }
            this.EfficiencyAccelerationTestViewModel = efficiencyAccelerationTestViewModel;
            if (ReferenceEquals(dataTimeTestViewModel, null))
            {
                throw new ArgumentNullException("dataTimeTestViewModel");
            }
            this.DataTimeTestViewModel = dataTimeTestViewModel;
            if (ReferenceEquals(algorithms, null))
            {
                throw new ArgumentNullException("algorithms");
            }
            this.Algorithms = algorithms;

            if (ReferenceEquals(configurationService, null))
            {
                throw new ArgumentNullException("configurationService");
            }
            _configurationService = configurationService;

            algorithms.ChangeAlgorithm += (s, e) => EfficiencyAccelerationTestViewModel.Algorithm = e.EventInfo;
            algorithms.ChangeAlgorithm += (s, e) => DataTimeTestViewModel.Algorithm = e.EventInfo;
        }

        #endregion

        #region Methods

        private void OnConfiguration()
        {
            var window = new ConfigurationWindow();
            var viewModel = new ConfigurationViewModel(this.Dispatcher, _configurationService);
            window.DataContext = viewModel;
            viewModel.RequestClose += (s, e) => window.Close();
            window.Owner = Application.Current.MainWindow;
            window.ShowDialog();
        }


        #endregion  
    }
}
